﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class Success : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                conn.Open();
                //insert new virtual shop
                SqlCommand cmdIn = new SqlCommand("INSERT INTO Virtual_Shop VALUES('" + Session["ID"] + "', '#FFFFFF', 'Arial', '1')", conn);
                cmdIn.ExecuteNonQuery();

                //get virtual shop id
                SqlCommand cmdSelect = new SqlCommand("SELECT v_id FROM Virtual_Shop WHERE u_id='" + Session["ID"] + "'", conn);
                String id = cmdSelect.ExecuteScalar().ToString();

                //get virtual shop charge
                cmdSelect = new SqlCommand("SELECT ch_price FROM Charges WHERE ch_type='Virtual'", conn);
                Double price = Convert.ToDouble(cmdSelect.ExecuteScalar());

                //insert into shop charges
                cmdIn = new SqlCommand("INSERT INTO Shop_Charges VALUES('" + id + "', '" + Session["ID"] + "', @sdate, @edate, '" + price + "')", conn);
                DateTime now = DateTime.Now;
                cmdIn.Parameters.AddWithValue("@sdate", now);
                now = now.AddYears(1);
                cmdIn.Parameters.AddWithValue("@edate", now);
                cmdIn.ExecuteNonQuery();

                //update user position
                SqlCommand cmdUpdate = new SqlCommand("UPDATE Users SET u_position='ShopOwner' WHERE u_id='" + Session["ID"] + "'", conn);
                cmdUpdate.ExecuteNonQuery();
                Session["Role"] = "ShopOwner";

                conn.Close();
                Label1.Text = "You can view <a href='/VirtualShop.aspx?id=" + id + "'>Your Virtual Shop</a> or <a href='/VirtualCustomize.aspx'>Customize</a> it now! :)";
            }
        }
    }
}